unit UPermissoes;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Db, DBTables, DBCtrls, Grids, DBGrids,
  RxLookup,UFomCadastro, CheckLst,Menus, DBClient, Provider, ComCtrls, Variants;

type
  TFPermissao = class(TFormCadastro)
    Label1: TLabel;
    DBLGrupoUsuario: TDBLookupComboBox;
    GBPermissoes: TGroupBox;
    CkLBPermissao: TCheckListBox;
    QGrupoUsuarioPermissao: TQuery;
    DSGrupoUsuarioPermissao: TDataSource;
    cdsCadastroNIV_CODIGO: TIntegerField;
    cdsCadastroNIV_NOME: TStringField;
    cdsCadastroUSUARIO_CADASTRO: TStringField;
    cdsCadastroUSUARIO_AUTERACAO: TStringField;
    cdsCadastroUSUARIO_EXCLUSAO: TStringField;
    cdsCadastroDATA_CADASTRO: TDateTimeField;
    cdsCadastroDATA_EXCLUSAO: TDateTimeField;
    cdsCadastroDATA_AUTERACAO: TDateTimeField;
    cdsCadastroEXCLUIDO: TStringField;
    GroupBox1: TGroupBox;
    cbInserir: TCheckBox;
    cbEditar: TCheckBox;
    cbExcluir: TCheckBox;
    GroupBox2: TGroupBox;
    cbPesquisa: TCheckBox;
    cbCadastro: TCheckBox;
    cbUsuario: TCheckBox;
    btnSalvarFuncAbas: TBitBtn;
    CbMarcarTodos: TCheckBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);

    Procedure Cria_Arvore(Cod_GR: String);

    Function Pega_Codigo_Permissao(Nome: String) : Integer;
    procedure BtnEditarClick(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
    procedure BtnCancelarClick(Sender: TObject);
    procedure DSGrupoUsuarioPermissaoStateChange(Sender: TObject);
    procedure CbMarcarTodosClick(Sender: TObject);

    Procedure Carrega_Permissoes(Cod_GR: String);
    procedure DSGrupoUsuarioPermissaoDataChange(Sender: TObject;
      Field: TField);
    procedure dsCadastroDataChange(Sender: TObject; Field: TField);
    procedure btnSalvarFuncAbasClick(Sender: TObject);
    procedure dsCadastroStateChange(Sender: TObject);
    procedure CkLBPermissaoClick(Sender: TObject);
    procedure cbInserirClick(Sender: TObject);
    procedure CkLBPermissaoClickCheck(Sender: TObject);
    

  private
    { Private declarations }
    procedure carrega_funcoes_abas(nivel, permissao : string);
  public
    { Public declarations }
  end;

Const Verifica_Permissao_GR_Usuario = ' SELECT * FROM NIVELPERMISSAO                            '+
                                      ' WHERE (NIVEL_PER_NIVEL = %s) AND (NIVEL_PER_PERMISSAO = %s) ';

var
  FPermissao    : TFPermissao;
  Codigos_Menus : TStringlist;

implementation

uses UGeral, UCadastroPermissao;

{$R *.DFM}

procedure TFPermissao.Cria_Arvore(Cod_GR: String);
Var A,B,C,D,E,F,G,Cont : Integer;
    Nome_Menu,Caption_Menu : String;
begin

  if Trim(Cod_GR) = '' then
    Cod_GR := '0';

  CkLBPermissao.Items.Clear;

  Codigos_Menus := nil;

  Codigos_Menus := TStringList.Create;

  Cont := -1;

  For A := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items.Count - 1 do
  Begin

    Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Caption,'&','',[]);
    Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Name;

    if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
    begin

      Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

      CkLBPermissao.Items.Add(Caption_Menu);
      Inc(Cont);

      If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
        CkLBPermissao.Checked[Cont] := true
      else
        CkLBPermissao.Checked[Cont] := False;

    end;

    For B := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Count - 1 do
    Begin

      Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Caption,'&','',[]);
      Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Name;

      if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
      begin

        Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

        CkLBPermissao.Items.Add('   '+Caption_Menu);
        Inc(Cont);

        If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
          CkLBPermissao.Checked[Cont] := true
        else
          CkLBPermissao.Checked[Cont] := False;

      end;

      For C := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Count - 1 do
      Begin

        Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Caption,'&','',[]);
        Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Name;

        if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
        begin

          Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

          CkLBPermissao.Items.Add('      '+Caption_Menu);
          Inc(Cont);

          If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
            CkLBPermissao.Checked[Cont] := true
          else
            CkLBPermissao.Checked[Cont] := False;

        end;

        For D := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Count - 1 do
        Begin

          Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Caption,'&','',[]);
          Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Name;

          if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
          begin

            Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

            CkLBPermissao.Items.Add('         '+Caption_Menu);
            Inc(Cont);

            If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
              CkLBPermissao.Checked[Cont] := true
            else
              CkLBPermissao.Checked[Cont] := False;

          end;

          For E := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Count - 1 do
          Begin

            Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Caption,'&','',[]);
            Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Name;

            if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
            begin

              Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

              CkLBPermissao.Items.Add('            '+Caption_Menu);
              Inc(Cont);

              If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                CkLBPermissao.Checked[Cont] := true
              else
                CkLBPermissao.Checked[Cont] := False;

            end;

            For F := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Count - 1 do
            Begin

              Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Caption,'&','',[]);
              Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Name;

              if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
              begin

                Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

                CkLBPermissao.Items.Add('               '+Caption_Menu);
                Inc(Cont);

                If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                  CkLBPermissao.Checked[Cont] := true
                else
                  CkLBPermissao.Checked[Cont] := False;

              end;

              For G := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Count - 1 do
              Begin

                Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Items[G].Caption,'&','',[]);
                Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Items[G].Name;

                if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
                begin

                  Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

                  CkLBPermissao.Items.Add('                  '+Caption_Menu);
                  Inc(Cont);

                  If Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                    CkLBPermissao.Checked[Cont] := true
                  else
                    CkLBPermissao.Checked[Cont] := False;

                end;

              end; // FOR G

            end; // FOR F

          end; // FOR E

        end; // FOR D

      end; // FOR C

    end; // FOR B

  end; // FOR A

 // Showmessage(Nomes_Menus.Strings[1]); 

end;


procedure TFPermissao.FormClose(Sender: TObject; var Action: TCloseAction);
begin

 Codigos_Menus := nil;

 FPermissao := nil;

 inherited;

end;

procedure TFPermissao.FormCreate(Sender: TObject);
begin
  QCadastro.SQL.Clear;
  QCadastro.SQL.Add('SELECT * FROM NIVEL');
  inherited;

  DSGrupoUsuarioPermissao.DataSet.Open;

  Cria_Arvore(DSGrupoUsuarioPermissao.DataSet.Fields[0].asstring);
  {
  FPermissao.Constraints.MaxWidth  := 514;
  FPermissao.Constraints.MinWidth  := 514;
  FPermissao.Constraints.MaxHeight := 557;
  FPermissao.Constraints.MinHeight := 557;
  }
end;

function TFPermissao.Pega_Codigo_Permissao(Nome: String): Integer;
Const Seleciona_Permissao = ' SELECT * FROM PERMISSAO WHERE PER_TRANSACAO = %s ';
begin

  Usando_Query_Pesquisa(Format(Seleciona_Permissao,[QuotedStr(Nome)]));

  Result := Query_Pesquisa.Fieldbyname('PER_CODIGO').AsInteger;

end;

procedure TFPermissao.BtnEditarClick(Sender: TObject);
begin
  inherited;
  DSGrupoUsuarioPermissao.DataSet.Edit;
  DBLGrupoUsuario.Enabled := false;
  
end;

procedure TFPermissao.BtnSalvarClick(Sender: TObject);
Const
  select_permissao = 'SELECT * FROM NIVELPERMISSAO WHERE (NIVEL_PER_NIVEL = %s) AND(NIVEL_PER_PERMISSAO = %s)';
  delete_permissao = 'DELETE FROM NIVELPERMISSAO  WHERE (NIVEL_PER_NIVEL = %s) AND(NIVEL_PER_PERMISSAO  = %s)';
  Inseri_Permissao =  'INSERT INTO NIVELPERMISSAO ('#10+
                      '  NIVEL_PER_NIVEL,'#10+
                      '  NIVEL_PER_PERMISSAO,'#10+
                      '  NIVELPERMISSAO.FUNCOES,'#10+
                      '  NIVELPERMISSAO.ABAS)'#10+
                      'VALUES (%s,%s, ''In,Ed,Ex'',''P,C,U'')';

var Cod_GR : String;
    I : Integer;

begin

  CkLBPermissao.ItemIndex := 0;
  
  Cod_GR := DBLGrupoUsuario.KeyValue;

  if Trim(Cod_GR) = '' then
    Exit;

  //executaSQL(Format(Deleta_Permissoes,[Cod_GR]));

  for I := 0 to Codigos_Menus.Count - 1 do
  begin

    try

      if not(Usando_Query_Pesquisa(Format(select_permissao, [Cod_GR,Codigos_Menus[I]]))) then
      begin

        if CkLBPermissao.Checked[I] = True then
          executaSQL(Format(Inseri_Permissao,[Cod_GR,Codigos_Menus[I]]))

      end else begin

        if CkLBPermissao.Checked[I] = False then
          executaSQL(Format(delete_permissao,[Cod_GR,Codigos_Menus[I]]));

      end;

    except
    end;

    Update;

  end;

  DSGrupoUsuarioPermissao.DataSet.Post;

  if btnSalvarFuncAbas.Enabled then
    btnSalvarFuncAbas.Click;

  inherited;

end;

procedure TFPermissao.BtnCancelarClick(Sender: TObject);
begin
  inherited;
  DSGrupoUsuarioPermissao.DataSet.Cancel;
end;

procedure TFPermissao.DSGrupoUsuarioPermissaoStateChange(Sender: TObject);
begin
  inherited;

  GBPermissoes.Enabled := not(DSGrupoUsuarioPermissao.DataSet.State in [DsBrowse]);

end;

procedure TFPermissao.CbMarcarTodosClick(Sender: TObject);
const
  deleteNivelPermissao =
    'DELETE FROM NIVELPERMISSAO'#10+
    'WHERE'#10+
    '  (NIVELPERMISSAO.NIVEL_PER_NIVEL = %s)AND'#10+
    '  (NIVELPERMISSAO.NIVEL_PER_PERMISSAO <> (SELECT PERMISSAO.PER_CODIGO FROM PERMISSAO WHERE PERMISSAO.PER_TRANSACAO = %s))';

  updateNivelPermissao =
    'UPDATE NIVELPERMISSAO SET'#10+
    '   NIVELPERMISSAO.USUARIO_AUTERACAO = %s,'#10+
    '   NIVELPERMISSAO.DATA_AUTERACAO    = %s,'#10+
    '   NIVELPERMISSAO.FUNCOES           = %s,'#10+
    '   NIVELPERMISSAO.ABAS              = %s'#10+
    'WHERE'#10+
    '  (NIVELPERMISSAO.NIVEL_PER_NIVEL = %s)AND'#10+
    '  (NIVELPERMISSAO.NIVEL_PER_PERMISSAO <> (SELECT PERMISSAO.PER_CODIGO FROM PERMISSAO WHERE PERMISSAO.PER_TRANSACAO = %s))';

var
  I : Integer;

begin
  inherited;

  cbInserir.Checked := CbMarcarTodos.Checked;
  cbEditar.Checked  := CbMarcarTodos.Checked;
  cbExcluir.Checked := CbMarcarTodos.Checked;

  cbPesquisa.Checked := CbMarcarTodos.Checked;
  cbCadastro.Checked := CbMarcarTodos.Checked;
  cbUsuario.Checked  := CbMarcarTodos.Checked;

  for I := 0 to Codigos_Menus.Count - 1 do
    CkLBPermissao.Checked[I] := CbMarcarTodos.Checked;

  if CbMarcarTodos.Checked then
  begin

    executaSQL(Format(updateNivelPermissao,[QuotedStr(Usuario),
                                            QuotedStr(FormatDateTime('mm/dd/yyyy', now)),
                                            QuotedStr('In,Ed,Ex'),
                                            QuotedStr('P,C,U'),
                                            VarToStr(DBLGrupoUsuario.KeyValue),
                                            QuotedStr(Ultimo_menu)]));

  end else begin

    executaSQL(Format(deleteNivelPermissao,[VarToStr(DBLGrupoUsuario.KeyValue),
                                            QuotedStr(Ultimo_menu)]));

  end;

end;

procedure TFPermissao.Carrega_Permissoes(Cod_GR: String);
var I : Integer;
begin

  if Codigos_Menus <> nil then
  begin

    for I := 0 to Codigos_Menus.Count - 1 do
    begin

      if Usando_Query_Pesquisa(Format(Verifica_Permissao_GR_Usuario,[Cod_GR,Codigos_Menus.Strings[I]])) then
        CkLBPermissao.Checked[I] := True
      else
        CkLBPermissao.Checked[I] := False;

      Update;

    end;

  end;
  
end;


procedure TFPermissao.DSGrupoUsuarioPermissaoDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;

  If DSGrupoUsuarioPermissao.DataSet.State in [dsbrowse] then
    begin

      DBLGrupoUsuario.KeyValue := dsCadastro.DataSet.FieldByName('NIV_CODIGO').AsInteger;

      Update;

      Carrega_Permissoes(DBLGrupoUsuario.KeyValue);

    end;

end;

procedure TFPermissao.dsCadastroDataChange(Sender: TObject; Field: TField);
begin
  inherited;

  if dsCadastro.DataSet.State = dsBrowse then
  begin

    DBLGrupoUsuario.KeyValue := dsCadastro.DataSet.FieldByName('NIV_CODIGO').AsInteger;

    Update;

    Carrega_Permissoes(DBLGrupoUsuario.KeyValue);

  end;

end;

procedure TFPermissao.btnSalvarFuncAbasClick(Sender: TObject);
const
  updateNivelPermissao =
    'UPDATE NIVELPERMISSAO SET'#10+
    '   NIVELPERMISSAO.USUARIO_AUTERACAO = %s,'#10+
    '   NIVELPERMISSAO.DATA_AUTERACAO = %s,'#10+
    '   NIVELPERMISSAO.FUNCOES = %s,'#10+
    '   NIVELPERMISSAO.ABAS = %s'#10+
    'WHERE'#10+
    '  NIVEL_PER_NIVEL = %s AND NIVEL_PER_PERMISSAO = %s';

var
  funcoes, abas : string;

begin
  inherited;

  if cbInserir.Checked then
    funcoes := funcoes + 'In,';

  if cbEditar.Checked then
    funcoes := funcoes + 'Ed,';

  if cbExcluir.Checked then
    funcoes := funcoes + 'Ex';

  if cbPesquisa.Checked then
    abas := abas + 'P,';

  if cbCadastro.Checked then
    abas := abas + 'C,';

  if cbUsuario.Checked then
    abas := abas + 'U';

  executaSQL(Format(updateNivelPermissao,[QuotedStr(Usuario),
                                          QuotedStr(FormatDateTime('mm/dd/yyyy', now)),
                                          QuotedStr(funcoes),
                                          QuotedStr(abas),
                                          varToStr(DBLGrupoUsuario.KeyValue),
                                          Codigos_Menus[CkLBPermissao.ItemIndex]]));

  btnSalvarFuncAbas.Enabled := False;

end;

procedure TFPermissao.dsCadastroStateChange(Sender: TObject);
begin
  inherited;

  cbInserir.Enabled         := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  cbEditar.Enabled          := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  cbExcluir.Enabled         := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  cbPesquisa.Enabled        := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  cbCadastro.Enabled        := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  cbUsuario.Enabled         := dsCadastro.DataSet.State in[dsInsert, dsEdit];
  btnSalvarFuncAbas.Enabled := dsCadastro.DataSet.State in[dsInsert, dsEdit];

end;

procedure TFPermissao.carrega_funcoes_abas(nivel, permissao: string);
const
  SelectNivelPermissao =
    'SELECT'#10+
    '   NIVELPERMISSAO.FUNCOES,'#10+
    '   NIVELPERMISSAO.ABAS'#10+
    'FROM'#10+
    '  NIVELPERMISSAO'#10+
    'WHERE'#10+
    '  NIVEL_PER_NIVEL = %s AND NIVEL_PER_PERMISSAO = %s';

begin

  if Usando_Query_Pesquisa(Format(SelectNivelPermissao, [nivel, permissao])) then
  begin

    cbInserir.Checked := pos('In', Query_Pesquisa.FieldByName('FUNCOES').AsString) <> 0;
    cbEditar.Checked  := pos('Ed',  Query_Pesquisa.FieldByName('FUNCOES').AsString) <> 0;
    cbExcluir.Checked := pos('Ex', Query_Pesquisa.FieldByName('FUNCOES').AsString) <> 0;

    cbPesquisa.Checked := pos('P', Query_Pesquisa.FieldByName('ABAS').AsString) <> 0;
    cbCadastro.Checked := pos('C', Query_Pesquisa.FieldByName('ABAS').AsString) <> 0;
    cbUsuario.Checked  := pos('U',  Query_Pesquisa.FieldByName('ABAS').AsString) <> 0;

  end else begin

    cbInserir.Checked := False;
    cbEditar.Checked  := False;
    cbExcluir.Checked := False;

    cbPesquisa.Checked := False;
    cbCadastro.Checked := False;
    cbUsuario.Checked  := False;

  end;

end;

procedure TFPermissao.CkLBPermissaoClick(Sender: TObject);
begin
  inherited;

  btnSalvarFuncAbas.Enabled := True;

  cbInserir.Enabled := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];
  cbEditar.Enabled  := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];
  cbExcluir.Enabled := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];

  cbPesquisa.Enabled := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];
  cbCadastro.Enabled := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];
  cbUsuario.Enabled  := CkLBPermissao.Checked[CkLBPermissao.ItemIndex];

  if CkLBPermissao.Checked[CkLBPermissao.ItemIndex] then
    carrega_funcoes_abas(VarToStr(DBLGrupoUsuario.KeyValue), Codigos_Menus[CkLBPermissao.ItemIndex]);

end;

procedure TFPermissao.cbInserirClick(Sender: TObject);
begin
  inherited;

  btnSalvarFuncAbas.Enabled := True;

end;

procedure TFPermissao.CkLBPermissaoClickCheck(Sender: TObject);
const
  Select_Permissao = 'SELECT * FROM NIVELPERMISSAO WHERE (NIVEL_PER_NIVEL = %s) AND(NIVEL_PER_PERMISSAO = %s)';
  Deleta_Permissao = ' DELETE FROM NIVELPERMISSAO WHERE (NIVEL_PER_NIVEL = %s) AND (NIVEL_PER_PERMISSAO = %s)';
  Inseri_Permissao =  'INSERT INTO NIVELPERMISSAO ('#10+
                      '  NIVEL_PER_NIVEL,'#10+
                      '  NIVEL_PER_PERMISSAO,'#10+
                      '  NIVELPERMISSAO.FUNCOES,'#10+
                      '  NIVELPERMISSAO.ABAS)'#10+
                      'VALUES (%s,%s, ''In,Ed,Ex'',''P,C,U'')';

begin
  inherited;

  if CkLBPermissao.Checked[CkLBPermissao.ItemIndex] then
  begin

    if not(Usando_Query_Pesquisa(Format(select_permissao, [DBLGrupoUsuario.KeyValue,Codigos_Menus[CkLBPermissao.ItemIndex]]))) then
      executaSQL(Format(Inseri_Permissao,[DBLGrupoUsuario.KeyValue ,Codigos_Menus[CkLBPermissao.ItemIndex]]))

  end else
    executaSQL(Format(Deleta_Permissao,[DBLGrupoUsuario.KeyValue ,Codigos_Menus[CkLBPermissao.ItemIndex]]));


end;

end.
